using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Example.Engine;
using Example.Engine.PluginBase;
using Example.Plugin1.Xml;
using DynamiteXml; 

namespace TestApplication
{
    class Program
    {
        static IPlugin myPlugin;
        static ItemWithXmlDS allItemsWithXml;

        static void Main()
        {
            LoadPlugin();

            allItemsWithXml = new ItemWithXmlDS();            

            List<Guid> recordsList = new List<Guid>();
            recordsList.Add(AddRecordWithParentXml("Parent_Record_1 (original)"));
            recordsList.Add(AddRecordWithParentXml("Parent_Record_1"));
            recordsList.Add(AddRecordWithParentXml("Parent_Record_2 (original)"));
            recordsList.Add(AddRecordWithParentXml("Parent_Record_2"));
            recordsList.Add(AddRecordWithDescendentXml("Descendent_Record_1 (original)"));
            recordsList.Add(AddRecordWithDescendentXml("Descendent_Record_1"));
            recordsList.Add(AddRecordWithDescendentXml("Descendent_Record_2 (original)"));
            recordsList.Add(AddRecordWithDescendentXml("Descendent_Record_2"));

            ModifyRecordWithParentInEngine(recordsList[1]);
            ModifyRecordWithParentInPlugin(recordsList[3]);
            ModifyRecordWithDescendentInEngine(recordsList[5]);
            ModifyRecordWithDescendentInPlugin(recordsList[7]);
        }

        private static void LoadPlugin()
        {
            // To make the example easier I load the plugin here setting the assemly name & class manually.
            Assembly pluginAssembly = Assembly.Load("Example.Plugin1");
            myPlugin = pluginAssembly.CreateInstance("Example.Plugin1.Plugin1") as BasePlugin;

        }

        private static void ModifyRecordWithDescendentInPlugin(Guid rowId)
        {
            ItemWithXmlDS.ItemWithXmlRow row = allItemsWithXml.ItemWithXml.FindByItemWithXmlId(rowId);
            myPlugin.ModifyRecordWithDescendentXml(row);
        }

        private static void ModifyRecordWithDescendentInEngine(Guid rowId)
        {
            ItemWithXmlDS.ItemWithXmlRow row = allItemsWithXml.ItemWithXml.FindByItemWithXmlId(rowId);
            ExecuteEngineCode.ModifyRecordWithDescendent(row);
        }

        private static void ModifyRecordWithParentInPlugin(Guid rowId)
        {
            ItemWithXmlDS.ItemWithXmlRow row = allItemsWithXml.ItemWithXml.FindByItemWithXmlId(rowId);
            myPlugin.ModifyRecordWithParentXml(row);
        }

        private static void ModifyRecordWithParentInEngine(Guid rowId)
        {
            ItemWithXmlDS.ItemWithXmlRow row = allItemsWithXml.ItemWithXml.FindByItemWithXmlId(rowId);
            ExecuteEngineCode.ModifyRecordWithParent(row);
        }

        private static Guid AddRecordWithParentXml(string description)
        {
            Guid result = Guid.NewGuid();

            ItemWithXmlDS.ItemWithXmlRow row = allItemsWithXml.ItemWithXml.NewItemWithXmlRow();
            row.ItemWithXmlId = result;
            row.Description = description;

            ParentXml xml = new ParentXml();
            xml.ParentProperty = 11; // Parent value = 1#

            row.Xml = DynamiteXmlLogic.Serialize(xml);

            allItemsWithXml.ItemWithXml.AddItemWithXmlRow(row);

            return result;
        }

        private static Guid AddRecordWithDescendentXml(string description)
        {
            Guid result = Guid.NewGuid();

            ItemWithXmlDS.ItemWithXmlRow row = allItemsWithXml.ItemWithXml.NewItemWithXmlRow();
            row.ItemWithXmlId = result;
            row.Description = description;

            DescendentXml xml = new DescendentXml();
            xml.ParentProperty = 21; // Descendent value = 2#
            xml.DescendentProperty = "New descendent property";

            row.Xml = DynamiteXmlLogic.Serialize(xml);

            allItemsWithXml.ItemWithXml.AddItemWithXmlRow(row);

            return result;
        }
    }
}
